/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or ( at your option ) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#pragma once

class CLineBuffer
{
private:
	PBYTE	_pVal;
	PBYTE	_pTok;
	WORD	_cbVal;
	WORD	_cbUsed;

	BOOLEAN _resizeBuf( const WORD cbReq );

public:
	CLineBuffer();
	~CLineBuffer();
	
	WORD operator = ( const CHAR *szVal );

	WORD operator + ( const CHAR *szVal );
	WORD operator + ( const WCHAR *wszVal );
	WORD operator + ( const CHAR cVal );
	WORD operator + ( const BYTE bVal );
	WORD operator + ( const SHORT sVal );
	WORD operator + ( const WORD wVal );
	WORD operator + ( const LONG lVal );
	WORD operator + ( const DWORD dVal );

	WORD GetLength();
	LPCSTR GetBuffer();

	VOID TruncToLength( WORD cbLength );
	VOID Truncate( WORD count );
	VOID TruncateSMS();
	
	VOID fput( FILE *outfile );
	VOID fputEncoded( FILE *outFile );
	INT  fgetEncoded( FILE *inFile );

	WORD GetTokenFirst( const CHAR delim, CLineBuffer * pBuf );
	WORD GetTokenNext( const CHAR delim, CLineBuffer * pBuf );
	INT	 DBWriteTokenFirst( HANDLE hContact, const CHAR* szModule, const CHAR* szSetting, const CHAR delim );
	INT	 DBWriteTokenNext( HANDLE hContact, const CHAR* szModule, const CHAR* szSetting, const CHAR delim );
	INT	 DBWriteSettingString( HANDLE hContact, const CHAR* szModule, const CHAR* szSetting );
};

class CVCardFileVCF
{
private:
	CLineBuffer		 _clVal;
	FILE			*_pFile;
	HANDLE			 _hContact;
	const CHAR		*_pszBaseProto;
	WORD			 _cbRew;
	BOOLEAN			 _useUtf8;
	WORD			 _hasUtf8;

	WORD	packList( LPIDSTRLIST pList, UINT nList, INT iID, WORD *cbRew = NULL );
	BOOLEAN GetSetting( const CHAR *szModule, const CHAR *szSetting, DBVARIANT *dbv );
	WORD	packDB( const CHAR *szModule, const CHAR *szSetting, WORD *cbRew = NULL );
	WORD	packDBList( const CHAR *szModule, const CHAR *szSetting, MIRSERVICE GetList, BOOLEAN bSigned = FALSE, WORD *cbRew = NULL );
	
	VOID	writeLine( const CHAR *szSet, WORD *cbRew = NULL );
	VOID	writeLineEncoded( const CHAR *szSet, WORD *cbRew = NULL );
	INT		readLine( LPSTR szVCFSetting, WORD cchSetting );

public:
	CVCardFileVCF();

	BOOLEAN Open( HANDLE hContact, LPCSTR pszFileName, LPCSTR pszMode );
	VOID	Close( VOID );
	BOOLEAN Export( BOOLEAN bExportUtf );
	BOOLEAN Import();
};